package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.IBusinessUserService;
import com.mdd.admin.validate.BusinessUserSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.BusinessUserListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.BusinessUser;
import com.mdd.common.entity.LegalSalesmanClockOn;
import com.mdd.common.mapper.BusinessUserMapper;
import com.mdd.common.mapper.LegalSalesmanClockOnMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 总台账号实现类
 * @author LikeAdmin
 */
@Service
public class BusinessUserServiceImpl implements IBusinessUserService {
        
    @Resource
    BusinessUserMapper businessUserMapper;

    @Resource
    LegalSalesmanClockOnMapper legalSalesmanClockOnMapper;


    /**
     * 总台账号列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<BusinessUserListedVo>
     */
    @Override
    public PageResult<BusinessUserListedVo> list(PageValidate pageValidate, BusinessUserSearchValidate searchValidate) {

        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        MPJQueryWrapper<BusinessUser> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(BusinessUser.class)
                .select("t.`id`, t2.`name` as `areas`")
                .leftJoin("`legal_areas` t2 on t.`a_id` = t2.`id`")
                .eq("t.`role_id`", 3)
                .orderByDesc("t.`id`");
        businessUserMapper.setSearch(mpjQueryWrapper, searchValidate, new String[]{
            "like:nickname@t.`nickname`:str",
        });

        IPage<BusinessUserListedVo> iPage = businessUserMapper.selectJoinPage(
                new Page<>(page, limit), BusinessUserListedVo.class, mpjQueryWrapper);
        for (BusinessUserListedVo vo : iPage.getRecords()) {
            if (StringUtils.isEmpty(vo.getAreas())) {
                vo.setAreas("未知");
            }
            String curDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            QueryWrapper<LegalSalesmanClockOn> wrapper = new QueryWrapper<LegalSalesmanClockOn>()
                    .eq("user_id", vo.getId())
                    .eq("date", curDate);
            LegalSalesmanClockOn clock = legalSalesmanClockOnMapper.selectOne(wrapper);
            if (clock != null) {
                vo.setClockStatus("已打卡");
                vo.setDateTime(new SimpleDateFormat("yyyy-MM-dd").format(clock.getDate())
                        + " " + new SimpleDateFormat("HH:mm:ss").format(clock.getTime()));
//                vo.setDateTime();
            } else {
                vo.setClockStatus("未打卡");
                vo.setDateTime("-");
            }
            System.out.println("999");
        }
        return PageResult.iPageHandle(iPage);
    }

    @Override
    public List<BusinessUserListedVo> allBusiness() {
        MPJQueryWrapper<BusinessUser> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(BusinessUser.class)
                .eq("t.`role_id`", 3);
        return businessUserMapper.selectJoinList(BusinessUserListedVo.class, mpjQueryWrapper);
    }
}
